import * as UserApi from '@/api/member/user.js';
import {
  LOGIN_STATUS,
  UID,
  OPENID,
  PLATFORM,
  SHARE_INFO
} from '../../config/cache';
import Cache from '../../utils/cache';
import {
  USER_INFO
} from '../../config/cache';

const state = {
  token: Cache.get(LOGIN_STATUS) || '',
  backgroundColor: "#fff",
  userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
  uid: Cache.get(UID) || null,
  openid: Cache.get(OPENID) || null,
  homeActive: false,
  chatUrl: Cache.get('chatUrl') || '',
  systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : '',
  productType: Cache.get('productType') || '',
  dicData: Cache.get('dictData') || null,
  jumpParams: Cache.get('jumpParams') || null,
  urlParams: Cache.get('SET_URL_PARAMS') || null,
  shareInfo: Cache.get(SHARE_INFO) || null,
  // tabbarIndex
  tabBarIndex: Cache.get('TAB_BAR_IDX') || null,
  TAB_BAR_NEXT_URL: Cache.get('TAB_BAR_NEXT_URL') || null
};

const mutations = {
  LOGIN(state, opt) {
    state.token = opt.token;
    Cache.set(LOGIN_STATUS, opt.token);
  },
  SETUID(state, val) {
    state.uid = val;
    Cache.set(UID, val);
  },

  OPENID(state, val) {
    state.openid = val;
    Cache.set(OPENID, val);
  },

  UPDATE_LOGIN(state, token) {
    state.token = token;
  },

  LOGOUT(state) {
    state.token = undefined;
    state.uid = undefined
    Cache.clear(LOGIN_STATUS);
    Cache.clear(UID);
    Cache.clear(OPENID);
    Cache.clear(USER_INFO);
  },

  UPDATE_USERINFO(state, userInfo) {
    state.userInfo = userInfo;
    Cache.set(USER_INFO, userInfo);
  },

  OPEN_HOME(state) {
    state.homeActive = true;
  },

  CLOSE_HOME(state) {
    state.homeActive = false;
  },

  SET_CHATURL(state, chatUrl) {
    state.chatUrl = chatUrl;
  },

  SYSTEM_PLATFORM(state, systemPlatform) {
    state.systemPlatform = systemPlatform;
    Cache.set(PLATFORM, systemPlatform);
  },

  /**
   * 保存邀请人信息
   * @param {
   *   "code":"",
   *   "userId":"",
   *   expireTime:"2024-10-30 10:00:00"
   *  }
   */
  SET_SHARE_CODE(state, data) {
    state.shareInfo = data;
    if (data===null||data===undefined||data==='null'){
      Cache.clear(SHARE_INFO);
    }else {
      Cache.set(SHARE_INFO, data);
    }
  },

  // 字典
  DICT_DATA(state, map) {
    // Cache.clear('dictData');
    // console.log('map', map);
    let dataOj = state.dicData == null ? {} : state.dicData;

    if (typeof dataOj === 'string') {
      dataOj = JSON.parse(dataOj)
    }
    console.log('data', dataOj);
    // let dataOj = JSON.parse(data);
    for (let [key, value] of map) {
      delete dataOj[key]
      dataOj[key] = value;
    }
    state.dicData = dataOj;
    console.log('dataOj', dataOj);
    Cache.set('dictData', dataOj);
  },

  // 支付
  SET_JUMP_INFO(state, data) {
    if (data === null) {
      Cache.clear('jumpParams');
      return;
    }
    state.jumpParams = data;
    Cache.set('jumpParams', data);
  },

  // 设置url跳转参数
  SET_URL_PARAMS(state, data) {
    if (data === null) {
      Cache.clear('SET_URL_PARAMS');
      return;
    }
    state.urlParams = data;
    Cache.set('SET_URL_PARAMS', data);
  },

  SET_TAB_BAR_IDX(state, data) {
    state.tabBarIndex = data;
    Cache.set('TAB_BAR_IDX', data);
  },
  // 记录从当前tabbar页面路由去的那个页面；辅助恢复 当前tabbar选上
  SET_TAB_BAR_NEXT_URL(state, data) {
    state.tabBarIndex = data;
    Cache.set('TAB_BAR_NEXT_URL', data);
  }
};

const actions = {
  USERINFO({
    state,
    commit
  }, force) {
    return new Promise(reslove => {
      UserApi.getUserInfo().then(res => {
        commit("UPDATE_USERINFO", res.data);
        commit("SETUID", res.data.id);
        reslove(res.data);
      })
    }).catch(() => {

    });
    // debugger
    // if (state.userInfo !== null && !force)
    // 	return Promise.resolve(state.userInfo);
    // else
    // 	return new Promise(reslove => {
    // 		getUserInfo().then(res => {
    // 			commit("UPDATE_USERINFO", res.data);
    // 			reslove(res.data);
    // 		});
    // 	}).catch(() => {

    // 	});
  }
};

export default {
  state,
  mutations,
  actions
};
